package com.sugar.subscribe.course.module.business.controller.admin.coachservice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.coachservice.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachservice.CoachServiceDO;
import com.sugar.subscribe.course.module.business.service.coachservice.CoachServiceService;

@Tag(name = "管理后台 - 教练服务")
@RestController
@RequestMapping("/business/coach-service")
@Validated
public class CoachServiceController {

    @Resource
    private CoachServiceService coachServiceService;

    @PostMapping("/create")
    @Operation(summary = "创建教练服务")
    @PreAuthorize("@ss.hasPermission('business:coach-service:create')")
    public CommonResult<Long> createCoachService(@Valid @RequestBody CoachServiceSaveReqVO createReqVO) {
        return success(coachServiceService.createCoachService(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教练服务")
    @PreAuthorize("@ss.hasPermission('business:coach-service:update')")
    public CommonResult<Boolean> updateCoachService(@Valid @RequestBody CoachServiceSaveReqVO updateReqVO) {
        coachServiceService.updateCoachService(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教练服务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:coach-service:delete')")
    public CommonResult<Boolean> deleteCoachService(@RequestParam("id") Long id) {
        coachServiceService.deleteCoachService(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除教练服务")
                @PreAuthorize("@ss.hasPermission('business:coach-service:delete')")
    public CommonResult<Boolean> deleteCoachServiceList(@RequestParam("ids") List<Long> ids) {
        coachServiceService.deleteCoachServiceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教练服务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:coach-service:query')")
    public CommonResult<CoachServiceRespVO> getCoachService(@RequestParam("id") Long id) {
        CoachServiceDO coachService = coachServiceService.getCoachService(id);
        return success(BeanUtils.toBean(coachService, CoachServiceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教练服务分页")
    @PreAuthorize("@ss.hasPermission('business:coach-service:query')")
    public CommonResult<PageResult<CoachServiceRespVO>> getCoachServicePage(@Valid CoachServicePageReqVO pageReqVO) {
        PageResult<CoachServiceDO> pageResult = coachServiceService.getCoachServicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CoachServiceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教练服务 Excel")
    @PreAuthorize("@ss.hasPermission('business:coach-service:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoachServiceExcel(@Valid CoachServicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CoachServiceDO> list = coachServiceService.getCoachServicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教练服务.xls", "数据", CoachServiceRespVO.class,
                        BeanUtils.toBean(list, CoachServiceRespVO.class));
    }

}